#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/time.h>
 
/**
 * Returns the current time in microseconds.
 */
long getMicrotime(){
    struct timeval timestamp;
    gettimeofday(&timestamp, NULL);
	return timestamp.tv_usec;
}

/**
  * @Name    zoom_bitmap
  * @brief    16*16 放大到 32*32
  * @param   input_map: [输入/出] 
**			 width: [输入/出] 8的倍数
**			 height: [输入/出] 8的倍数
**			 out_map: [输入/出] 
**			 rate: [输入/出] 只适用于2倍
  * @retval
  * @Data    2021-03-25
 **/

static void zoom_bitmap(uint8_t *input_map, uint8_t width ,uint8_t height, uint8_t *out_map, uint8_t rate)
{
    uint8_t in_byte_len = width * height >> 3;
    uint8_t out_byte_len = in_byte_len * rate * rate;
    uint8_t width_byte = (width >> 3);  /*宽 转换为字节数*/
    uint8_t i,j,k,tmp_byte,index;

    if(input_map == out_map)    /*同一指针 将数据cpy 到最后*/
    {
        memcpy(out_map + out_byte_len - in_byte_len, input_map, in_byte_len);
        input_map = out_map + out_byte_len - in_byte_len;
    }
    
    for(j = 0; j < height; j++)
    {    
        for(i = 0; i < width_byte; i++)
        {
			tmp_byte = input_map[i + j * width_byte];
			index = i * rate + j * width_byte * rate * rate;

			out_map[index] = 0;
			out_map[index + width_byte * rate] = 0;
			out_map[index + 1] = 0;
			out_map[index + width_byte * rate + 1] = 0;

            for(k = 0; k < 8; k++)
            {
                if(tmp_byte & (0x80 >> k))
                {
					if(k < 4)
					{
						out_map[index] |= (0x80 >> (k * rate));									//左上
						out_map[index] |= (0x80 >> ((k * rate) + 1));							    //右上
						out_map[index + width_byte * rate] |= (0x80 >> (k * rate));				//左下
						out_map[index + width_byte * rate] |= (0x80 >> ((k * rate) + 1));		    //右下
					}
					else 
					{
						out_map[index + 1] |= (0x80 >> ((k - 4) * rate));								    //左上
						out_map[index + 1] |= (0x80 >> (((k - 4) * rate) + 1));							//右上
						out_map[index + width_byte * rate + 1] |= (0x80 >> ((k - 4) * rate));			    //左下
						out_map[index + width_byte * rate + 1] |= (0x80 >> (((k - 4) * rate) + 1));		//右下
					}
                }
            }
        }
    }
}


int main(void)
{
	//0x00,0x00,0x00,0x3E,0x66,0x66,0xC0,0xC0,0xC0,0xCF,0xC6,0x66,0x66,0x3C,0x00,0x00,/*"G",0*/
	///
	//	0x00,0x00,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x01,0x00,0x7F,0xFC,
	//	0x42,0x04,0x02,0x00,0xFF,0xFE,0x08,0x20,0x1C,0x40,0x03,0x80,0x0C,0x70,0x70,0x08/*"晏",0*/
	//

	uint8_t map_buff[132] = {
	0x00,0x00,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x01,0x00,0x7F,0xFC,
	0x42,0x04,0x02,0x00,0xFF,0xFE,0x08,0x20,0x1C,0x40,0x03,0x80,0x0C,0x70,0x70,0x08/*"晏",0*/
	};
	uint8_t width_byte = 2, height = 16, rate = 2;

	printf("hello word \n");

	for(int i = 0; i < height; i++)
	{
		for(int j = 0; j < width_byte; j++)
		{
			for(int k = 0; k < 8; k++)
			{
				if(map_buff[i * width_byte + j] & (0x80 >> k))
					printf("#");
				else
					printf(" ");
			}
		}
		printf("\n");
	}
	printf("S time: %lu \n ", getMicrotime());
	zoom_bitmap(map_buff, width_byte * 8, height, map_buff, rate);
	printf("E time: %lu \n ", getMicrotime());

	for(int i = 0; i < height * rate; i++)
	{
		for(int j = 0; j < width_byte * rate; j++)
		{
			for(int k = 0; k < 8; k++)
			{
				if(map_buff[i * width_byte * rate + j] & (0x80 >> k))
					printf("#");
				else
					printf(" ");
			}
		}
		printf("\n");
	}
	system("PAUSE");
}
